package com.example.alibabavodossservice.vod.transcodeTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import org.springframework.beans.BeanUtils;

import java.util.List;

import static com.example.alibabavodossservice.vod.InitVODConstants.VOD_CLIENT;
import static com.example.alibabavodossservice.vod.watermark.Cmd.buildEncryptConfig;
import static com.example.alibabavodossservice.vod.watermark.Cmd.buildOverrideParams;


/**
 * desc: 处理转码指令集
 *
 * @author xugenyin
 */
public class Cmd {


    /**
     * 提交媒体转码作业
     * 只有状态为上传完成、正常和审核中的视频才能发起转码。
     *
     * @param videoId          视频ID
     * @param tempLateGroupId  转码模板组ID
     * @param WatermarkId      水印ID
     * @param FileUrl          水印图片地址
     * @param WaterMarkContent 水印内容
     * @return SubmitTranscodeJobsResponse 提交媒体转码作业响应数据
     * @throws Exception 连接异常
     */
    public static SubmitTranscodeJobsResponse submitTranscodeJobs(String videoId, String tempLateGroupId, String WatermarkId, String FileUrl, String WaterMarkContent) throws Exception {
        SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
        //需要转码的视频ID
        request.setVideoId(videoId);
        //转码模板ID
        request.setTemplateGroupId(tempLateGroupId);
        //构建需要替换的水印参数（只有需要替换水印相关信息才需要构建）
        JSONObject overrideParams = buildOverrideParams(WatermarkId, FileUrl, WaterMarkContent);
        //覆盖参数，暂只支持水印部分参数替换（只有需要替换水印相关信息才需要传递）
        request.setOverrideParams(overrideParams.toJSONString());
        //构建标准加密配置参数（只有标准加密才需要构建）
        JSONObject encryptConfig = buildEncryptConfig(VOD_CLIENT);
        //HLS标准加密配置（只有标准加密才需要传递）
        request.setEncryptConfig(encryptConfig.toJSONString());
        return VOD_CLIENT.getAcsResponse(request);
    }


    /**
     * 创建转码模板组配置
     */
    public static AddTranscodeTemplateGroupResponse addTranscodeTemplateGroup(TranscodeTemplateGroupParameter transcodeTemplateGroupParameter) throws Exception {
        AddTranscodeTemplateGroupRequest transcodeTemplateGroupRequest = new AddTranscodeTemplateGroupRequest();
        BeanUtils.copyProperties(transcodeTemplateGroupParameter, transcodeTemplateGroupRequest);
        transcodeTemplateGroupRequest.setTranscodeTemplateList(buildTranscodeTemplateList().toJSONString());
        return VOD_CLIENT.getAcsResponse(transcodeTemplateGroupRequest);
    }

    /**
     * 设置转码模板为默认模板
     */
    public static void setDefaultTranscodeTemplateGroup(String transcodeTemplateGroupId) throws ClientException {
        SetDefaultTranscodeTemplateGroupRequest setDefaultTranscodeTemplateGroupRequest = new SetDefaultTranscodeTemplateGroupRequest();
        setDefaultTranscodeTemplateGroupRequest.setTranscodeTemplateGroupId(transcodeTemplateGroupId);
        VOD_CLIENT.getAcsResponse(setDefaultTranscodeTemplateGroupRequest);
    }

    /**
     * 构建需要添加的转码模板配置数据
     *
     * @return JSONArray 转码模板配置数据
     */
    public static JSONArray buildTranscodeTemplateList() {
        JSONObject transcodeTemplate = new JSONObject();
        //模板名称
        transcodeTemplate.put("TemplateName", "testtemplate");
        //清晰度
        transcodeTemplate.put("Definition", "LD");
        //视频流转码配置
        JSONObject video = new JSONObject();
        video.put("Width", 640);
        video.put("Bitrate", 400);
        video.put("Fps", 25);
        video.put("Remove", false);
        video.put("Codec", "H.264");
        video.put("Gop", "250");
        transcodeTemplate.put("Video", video);
        //音频流转码配置
        JSONObject audio = new JSONObject();
        audio.put("Codec", "AAC");
        audio.put("Bitrate", "64");
        audio.put("Channels", "2");
        audio.put("Samplerate", "32000");
        transcodeTemplate.put("Audio", audio);
        //封装容器
        JSONObject container = new JSONObject();
        container.put("Format", "mp4");
        transcodeTemplate.put("Container", container);
        //条件转码配置
        JSONObject transconfig = new JSONObject();
        transconfig.put("IsCheckReso", false);
        transconfig.put("IsCheckResoFail", false);
        transconfig.put("IsCheckVideoBitrate", false);
        transconfig.put("IsCheckVideoBitrateFail", false);
        transconfig.put("IsCheckAudioBitrate", false);
        transconfig.put("IsCheckAudioBitrateFail", false);
        transcodeTemplate.put("TransConfig", transconfig);
        //加密配置(只支持m3u8)
        //JSONObject encryptSetting = new JSONObject();
        //encryptSetting.put("EncryptType", "Private");
        //transcodeTemplate.put("EncryptSetting", encryptSetting);
        //水印ID(多水印关联)
        JSONArray watermarkIdList = new JSONArray();
        watermarkIdList.add("263261bdc1ff65782f8995c6dd22****");
        //USER_DEFAULT_WATERMARK 代表默认水印ID
        watermarkIdList.add("USER_DEFAULT_WATERMARK");
        transcodeTemplate.put("WatermarkIds", watermarkIdList);
        JSONArray transcodeTemplateList = new JSONArray();
        transcodeTemplateList.add(transcodeTemplate);
        return transcodeTemplateList;
    }

    /**
     * 修改转码模板组配置
     */
    public static UpdateTranscodeTemplateGroupResponse updateTranscodeTemplateGroup(TranscodeTemplateGroupParameter transcodeTemplateGroupParametr) throws Exception {
        UpdateTranscodeTemplateGroupRequest updateTranscodeTemplateGroupRequest = new UpdateTranscodeTemplateGroupRequest();
        BeanUtils.copyProperties(transcodeTemplateGroupParametr, updateTranscodeTemplateGroupRequest);
        updateTranscodeTemplateGroupRequest.setTranscodeTemplateList(buildTranscodeTemplateList().toJSONString());
        return VOD_CLIENT.getAcsResponse(updateTranscodeTemplateGroupRequest);
    }

    /**
     * 获取转码模板组配置信息
     *
     * @param transcodeTemplateGroupId 转码模板组ID
     * @return 转码组详细信息
     * @throws ClientException 连接异常
     */
    public static GetTranscodeTemplateGroupResponse.TranscodeTemplateGroup getTranscodeTemplateGroupInfo(String transcodeTemplateGroupId) throws ClientException {
        GetTranscodeTemplateGroupRequest getTranscodeTemplateGroupRequest = new GetTranscodeTemplateGroupRequest();
        getTranscodeTemplateGroupRequest.setTranscodeTemplateGroupId(transcodeTemplateGroupId);
        return VOD_CLIENT.getAcsResponse(getTranscodeTemplateGroupRequest).getTranscodeTemplateGroup();
    }

    /**
     * 获取转码模板组配置信息列表
     *
     * @param appId 应用ID
     * @return 转码组详细信息列表
     * @throws ClientException 连接异常
     */
    public static List<ListTranscodeTemplateGroupResponse.TranscodeTemplateGroup> getTranscodeTemplateGroupInfoList(String appId) throws ClientException {
        ListTranscodeTemplateGroupRequest listTranscodeTemplateGroupRequest = new ListTranscodeTemplateGroupRequest();
        listTranscodeTemplateGroupRequest.setAppId(appId);
        return VOD_CLIENT.getAcsResponse(listTranscodeTemplateGroupRequest).getTranscodeTemplateGroupList();
    }

    /**
     * 删除转码模版组
     *
     * @param transcodeTemplateGroupId 转码模板组id
     * @throws ClientException 连接异常
     */
    public static void deleteTranscodeTemplateGroup(String transcodeTemplateGroupId) throws ClientException {
        DeleteTranscodeTemplateGroupRequest deleteTranscodeTemplateGroupRequest = new DeleteTranscodeTemplateGroupRequest();
        deleteTranscodeTemplateGroupRequest.setTranscodeTemplateGroupId(transcodeTemplateGroupId);
        deleteTranscodeTemplateGroupRequest.setForceDelGroup("true");
        VOD_CLIENT.getAcsResponse(deleteTranscodeTemplateGroupRequest);
    }

    /**
     * 查询转码作业详情
     *
     * @param transcodeTaskId 转码作业ID
     * @return GetTranscodeTaskResponse 转码作业详情
     * @throws Exception 连接异常
     */
    public static GetTranscodeTaskResponse getTranscodeTask(String transcodeTaskId) throws Exception {
        GetTranscodeTaskRequest transcodeTaskRequest = new GetTranscodeTaskRequest();
        transcodeTaskRequest.setTranscodeTaskId(transcodeTaskId);
        return VOD_CLIENT.getAcsResponse(transcodeTaskRequest);
    }

    /**
     * 查询视频转码摘要信息
     *
     * @param videoIds 视频ID，多个用逗号分隔
     * @return
     * @throws Exception
     */
    public static GetTranscodeSummaryResponse getTranscodeSummary(String videoIds) throws Exception {
        GetTranscodeSummaryRequest transcodeSummaryRequest = new GetTranscodeSummaryRequest();
        transcodeSummaryRequest.setVideoIds(videoIds);
        return VOD_CLIENT.getAcsResponse(transcodeSummaryRequest);
    }

    /**
     * 查询转码任务列表
     * @param videoId 视频ID
     * @return ListTranscodeTaskResponse 转码任务列表
     * @throws Exception 连接异常
     */
     public  static ListTranscodeTaskResponse getTranscodeTaskList(String videoId) throws Exception {
         ListTranscodeTaskRequest listTranscodeTaskRequest = new ListTranscodeTaskRequest();
         listTranscodeTaskRequest.setVideoId(videoId);
         return VOD_CLIENT.getAcsResponse(listTranscodeTaskRequest);
     }

    /**
     * 提交转码作业
     * todo:后面可能提供可选择的转码模板组
     *
     * @param videoId 视频id
     * @return 提交转码作业响应
     * 2023/09/28
     */
    public static  SubmitTranscodeJobsResponse submitEncryptionJob(String videoId) throws ClientException {
         SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
//         request.setTemplateGroupId("deda82367c35e80918468434fbebf0b4"); hls h264    要搞一下后端解密-可能有bug
         request.setTemplateGroupId("b1156d2007bf5f23aec7e87e1124fa87"); //mp4 h264
         request.setVideoId(videoId);
         JSONObject encryptConfig = new JSONObject();
         encryptConfig.put("CipherText","N2FhYzBlY2EtMTNmNy00ZDhkLTk3OGItMmI3MzNhZTM0MGFkWXJ0RzQxS1IraDY0aERLYjVHNGRrMFJGR2VwSGpxa3RBQUFBQUFBQUFBQ2JuZXFhNWg5aC9tYVBIbXFmRUdtZUJDUklzanl1ODVJelFTQStjWHFaS1RoYXYwdnRUd3Rx");
         encryptConfig.put("DecryptKeyUri","http://127.0.0.1:8123/media-type/video-player?CipherText=N2FhYzBlY2EtMTNmNy00ZDhkLTk3OGItMmI3MzNhZTM0MGFkWXJ0RzQxS1IraDY0aERLYjVHNGRrMFJGR2VwSGpxa3RBQUFBQUFBQUFBQ2JuZXFhNWg5aC9tYVBIbXFmRUdtZUJDUklzanl1ODVJelFTQStjWHFaS1RoYXYwdnRUd3Rx");
         encryptConfig.put("KeyServiceType","KMS");
         request.setEncryptConfig(encryptConfig.toJSONString());
         return VOD_CLIENT.getAcsResponse(request);
     }




}
